package com.competition.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.competition.entity.ProductOrder;
import com.competition.entity.ReceiptInfo;
import com.competition.resp.ResponseResult;
import com.competition.service.ProductOrderService;
import com.competition.service.ReceiptInfoService;
import com.competition.utils.uuid.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/productOrder")
public class ProductOrderController {
    @Autowired
    ProductOrderService productOrderService;
    @Autowired
    ReceiptInfoService receiptInfoService;

    @GetMapping("/queryProductOrderList")
    public ResponseResult<Object> queryProductOrderList(@RequestParam HashMap<String,Object> params) {
        if(params.get("page")==null||params.get("pageSize")==null){
            return new ResponseResult<Object>(400,"参数错误",null);
        }
        int page = Integer.parseInt(params.get("page").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        HashMap<String, Object> reqMap = params;
        reqMap.put("limit",pageSize);
        reqMap.put("offset",(page - 1) * pageSize);
        List<ProductOrder> productOrders = productOrderService.queryProductOrderList(reqMap);
        long count = productOrderService.count();
        HashMap<String,Object> resMap = new HashMap<>();
        resMap.put("productOrders",productOrders);
        resMap.put("total",count);
        return  new ResponseResult<Object>(200,"获取订单列表成功",resMap);
    }
    @PutMapping("/modfiyProductOrder")
    @Transactional
    public ResponseResult<Object> modfiyProductOrder(@RequestBody ProductOrder productOrder) {
        if (productOrder.getProductOrderId() == null || productOrder.getReceiptInfo()==null || productOrder.getReceiptId()==null){
            return new ResponseResult<Object>(400,"参数错误",null);
        }
        ReceiptInfo receiptInfo = productOrder.getReceiptInfo();
        UpdateWrapper<ReceiptInfo> receiptInfoUpdateWrapper = new UpdateWrapper<>();
        receiptInfoUpdateWrapper.eq("receipt_id",receiptInfo.getReceiptId());
        receiptInfoService.update(receiptInfo,receiptInfoUpdateWrapper);
        return  new ResponseResult<Object>(200,"修改订单成功",null);
    }
    @PostMapping("/addProductOrder")
    public ResponseResult<Object> addProductOrder(@RequestBody HashMap<String,Object> params) {
        System.out.println(params);
        if(params.get("productId")==null || params.get("receiptId")==null||params.get("userId")==null){
            return new ResponseResult(300,"参数异常",null);
        }
        try {
            String productId = (String) params.get("productId");
            String receiptId = (String) params.get("receiptId");
            String userId = (String) params.get("userId");
            ProductOrder productOrder = new ProductOrder();
            productOrder.setProductOrderId(UUIDUtil.getUUID());
            productOrder.setProductId(productId);
            productOrder.setReceiptId(receiptId);
            productOrder.setUserId(userId);
            boolean save = productOrderService.save(productOrder);
            if (save){
                return new ResponseResult(200,"添加成功",null);
            }else {
                return new ResponseResult(300,"添加失败",null);
            }
        } catch (Exception e) {
            return new ResponseResult(300,"添加失败",null);
        }
    }
}
